﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Easy.Constant;
using Easy.Extend;
using Easy.MetaData;
using Easy.Web.CMS;
using Easy.Web.CMS.MetaData;
using Easy.Web.CMS.Widget;
using Microsoft.Practices.ServiceLocation;

namespace Easy.CMS.List.Models
{
    [DataConfigure(typeof(ListTopsWidgetMetaData)), Serializable]
    public class ListTopsWidget : WidgetBase
    {
        public string ListId { get; set; }

        public string FormCode { get; set; }

        public string ListCode { get; set; }

        /// <summary>
        /// 列表Title 字段截取长度
        /// </summary>
        public int? ItemLength { get; set; }

        public int? Tops { get; set; }
        public string SubTitle { get; set; }
        public string DetailPageUrl { get; set; }
    }

   class ListTopsWidgetMetaData : WidgetMetaData<ListTopsWidget>
   {
       protected override void ViewConfigure()
       {
           base.ViewConfigure();
           ViewConfig(m => m.SubTitle).AsTextBox().Order(NextOrder());
           ViewConfig(m => m.Tops).AsTextBox().Order(NextOrder()).RegularExpression(RegularExpression.PositiveIntegers);
          
           ViewConfig(m => m.MoreLink).AsTextBox().Order(NextOrder()).AddClass("select").AddProperty("data-url", Urls.SelectPage);
           ViewConfig(m => m.DetailPageUrl).AsTextBox().Order(NextOrder()).AddClass("select").AddProperty("data-url", Urls.SelectPage);

           ViewConfig(m => m.ItemLength).AsTextBox().Order(NextOrder()).RegularExpression(RegularExpression.PositiveIntegers).DefaultValue = 34;

           ViewConfig(m => m.ListCode).AsTextBox().ReadOnly().Order(NextOrder()).AddClass("select").AddProperty("data-url", Urls.SelectList);

           ViewConfig(m => m.FormCode).AsTextBox().ReadOnly().Order(NextOrder());

           ViewConfig(m => m.ListId).AsHidden();

           ViewConfig(m => m.PartialView).AsDropDownList().Order(NextOrder()).DataSource(() =>
           {
               var path = (ServiceLocator.Current.GetInstance<IApplicationContext>() as CMSApplicationContext).MapPath("~/Modules/List/Views");
               Dictionary<string, string> templates = new Dictionary<string, string>();
               Directory.GetFiles(path, "Widget.ListTops*.cshtml").Each(f =>
               {
                   string fileName = Path.GetFileNameWithoutExtension(f);
                   templates.Add(fileName, fileName.Replace("Widget.", ""));
               });
               return templates;
           });
       }
   }
}